<?php

/**
 * teacher actions.
 *
 * @package    sf_sandbox
 * @subpackage teacher
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class teacherActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        //$this->forward('default', 'module');
    }

    public function executeManagerTeachers(sfWebRequest $request) {
        $manager_teacher = $this->getUser()->getTeacherObject();
        $this->school = $manager_teacher->getSchool();
        $this->my_teachers = $this->school->getTeachers2(false);

        $this->my_hangged_teachers = $this->school->getTeachers2(true);
    }

    public function executeCard(sfWebRequest $request) {
        $this->setLayout('popupLayout');

        $id = $this->getRequestParameter('id');
        $this->teacher = TeacherTable::getInstance()->find($id);

        $this->forward404Unless($this->teacher);

        $this->teacher_form = new TeacherForm($this->teacher);
        unset($this->teacher_form['created_at'], $this->teacher_form['updated_at'], $this->teacher_form['created_by_user_id'], $this->teacher_form['updated_by_user_id'], $this->teacher_form['program_list'], $this->teacher_form['is_trainer']
        );
    }

    public function executeChangeHanging(sfWebRequest $request) {
        $this->checkHangTeacher();

        $this->status = $this->getRequestParameter('status');
        $this->teacher->setHangeStatus2($this->status);

        $this->redirect('teacher/managerTeachers');
    }

    public function executeHang(sfWebRequest $request) {
        $this->checkHangTeacher();

        $this->teacher->setAsHanged(false);
        $this->redirect('teacher/managerTeachers');
    }

    protected function checkHangTeacher() {

        $id = $this->getRequestParameter('teacher_id');
        $this->teacher = TeacherTable::getInstance()->find($id);

        $this->forward404Unless($this->teacher);

        $manager_teacher = $this->getUser()->getTeacherObject();
        $school = $manager_teacher->getSchool();

        $this->forward404Unless($this->teacher->getSchoolIntgId() == $school->getIntgId());
    }

    public function executeAdd(sfWebRequest $request) {
        $this->form = new PendingTeacherForm();
        if ($request->isMethod(sfWebRequest::POST)) {
            $values = $request->getParameter($this->form->getName());
            $school = $this->getUser()->getGuardUser()->getSchoolCustom();
            $values['school_intg_id'] = $school->getIntgId();
            $this->form->bind($values);
            if ($this->form->isValid()) {
                $this->form->save();
                $this->notice = 'teacher_added_pending_message';
            } else {
                $this->error = 'form_error';
            }
        }
    }

}
